package com.example.androidgeneric;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public abstract class GenericsListView<T extends User> extends ListFragment {

	public static final String PUT_DATA = "item";
	
	private ArrayList<T> list;
	private mAdapter adapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		list = setData();
		setList();
	}
	
	abstract public ArrayList<T> setData();
	
	private void setList() {
		mAdapter adapter = new mAdapter(getActivity(), android.R.layout.simple_list_item_1, getList());
		setListAdapter(adapter);
	}

	public ArrayList<T> getList() {
		return list;
	}

	public void setList(ArrayList<T> list) {
		this.list = list;
	}
	
	private class mAdapter extends ArrayAdapter<T> {

		public mAdapter(Context context, int resource, List<T> objects) {
			super(context, resource, objects);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			TextView text;
			
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
				convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
				text = (TextView) convertView.findViewById(android.R.id.text1);
				
				convertView.setTag(text);
			} else {
				text = (TextView) convertView.getTag();
			}
			
			text.setText(getItem(position).Name);
			
			return convertView;
		}
	}
}
